//
//  CarNewRecordCell.m
//  STool
//
//  Created by Samuel on 15/9/8.
//
//

#import "CarNewRecordCell.h"
#import "SAlertView.h"
#import "SCoreData.h"
@implementation CarNewRecordCell

- (void)awakeFromNib {
    self.time.dateDelegate = self;
    self.time.layer.masksToBounds = YES;
    self.time.layer.cornerRadius = 3;
    self.time.layer.borderWidth = .5;
    self.time.layer.borderColor = kUIColorFromRGB(0x829FF4).CGColor;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (IBAction)confiemAdding:(UIButton *)sender {
    if ([self.time.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"请选择时间"];
        return;
    }
    if ([self.allKM.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"总里程"];
        return;
    }
    if ([self.expendMoney.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"本次加油总价"];
        return;
    }
    if ([self.gasolineUnitCost.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"本次加油单价"];
        return;
    }
    if ([self.gasolineAvaCost.text isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"平均油耗"];
        return;
    }
    if (!self.delegate.carId && [self.delegate.carId isEqualToString:@""]) {
        [SAlertView showSAlterViewTitle:@"警告" Message:@"请选择车型后再选择添加"];
        return;
    }
    
    SCoreData *coreData = [[SCoreData sharedInstance]setTableName:@"CarRecord" andDatabaseName:@"CarDB"];
    NSMutableDictionary *saveData = [[NSMutableDictionary alloc]init];
    [saveData setObject:self.allKM.text forKey:@"alllKM"];
    
    [saveData setObject:self.delegate.carId forKey:@"carId"];
    [saveData setObject:self.gasolineAvaCost.text forKey:@"averageGlCost"];
    [saveData setObject:self.gasolineUnitCost.text forKey:@"glUnitCost"];
    [saveData setObject:self.time.timestamp forKey:@"recordTime"];
    [saveData setObject:self.expendMoney.text forKey:@"thisMoneyCost"];
    BOOL isInsertOk = [coreData insertCoreData:saveData andKey:nil];
    if (isInsertOk) {
        [self.delegate tableViewRemoveObjectAtIndexZero];
        [self.delegate getRecordDatas];
        [self cleanTextDatas];
        return;
    }
    [SAlertView showSAlterViewTitle:@"警告" Message:@"数据插入存储"];
}
- (void)cleanTextDatas
{
    self.time.text = self.allKM.text = self.expendMoney.text = self.gasolineAvaCost.text = self.gasolineUnitCost.text = @"";
}
@end
